﻿using MediatR;
using Warehouse.Api.Write.AllCation.Command.Shifing;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Write.AllCation.Hanlder
{
    public class DeleteShiftingCommandHanlder : IRequestHandler<DeleteShiftingCommand, ApiResult<int>>
    {
        public readonly IBaseRepostity<Shifting> stocktakingRepo;

        public DeleteShiftingCommandHanlder(IBaseRepostity<Shifting> stocktakingRepo)
        {
            this.stocktakingRepo = stocktakingRepo;
        }

        /// <summary>
        /// 删除移库和调拨信息表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(DeleteShiftingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var res = stocktakingRepo.GetAll().Where(x => x.Id == request.Id).First();
                res.IsDeleted = true;
                int r = stocktakingRepo.UpdateAsync(res).Result;
                if (r > 0)
                {
                    result.Msg = "删除成功";
                    result.Data = r;
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Msg = "删除失败";
                    result.Data = r;
                    result.Code = ApiEume.Error;
                }
            }
            catch (Exception ex)
            {

            }
            return Task.FromResult(result);
        }
    }
}
